﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class OutboundValidationDetailBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IOutboundValidationDetailDA OutboundValidationDetailDA = DataAccess.CreateOutboundValidationDetailDA();
        private PubParasInfo pubParasInfo;

        public OutboundValidationDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public OutboundValidationDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public OutboundValidationDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> id, List<string> lineID, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (OutboundValidationDetailDA.DeleteOutboundValidationDetail(dataBase, tran, id[0], lineID, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < lineID.Count; i++)
                    {
                        string CS00002 = temp;
                        temp = CS00002 + id[0] + "," + lineID[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return OutboundValidationDetailDA.GetAllOutboundValidationDetail(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            OutboundValidationDetailQueryEntity OutboundValidationDetail = obj as OutboundValidationDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            return OutboundValidationDetailDA.GetOutboundValidationDetailByQueryList(sqlWhere, OutboundValidationDetail, this.dataBaseID[0]);
        }

        public object GetDetailInfoByID(string id, string lineID, string pk3)
        {
            return OutboundValidationDetailDA.GetOutboundValidationDetailByID(id, lineID, this.dataBaseID[0]);
        }

        public List<object> GetDetailInfoListByID(string id, string subID)
        {
            List<OutboundValidationDetailInfo> OutboundValidationDetail = OutboundValidationDetailDA.GetOutboundValidationDetailByList(id, this.dataBaseID[0]);
            List<object> objs = null;
            if ((OutboundValidationDetail != null) && (OutboundValidationDetail.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < OutboundValidationDetail.Count; i++)
                {
                    objs.Add(OutboundValidationDetail[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            OutboundValidationDetailInfo OutboundValidationDetailInfo = obj as OutboundValidationDetailInfo;
            string lineID = string.Empty;
            try
            {
                if (OutboundValidationDetailInfo == null)
                {
                    return lineID;
                }
                OutboundValidationDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                OutboundValidationDetailInfo.UpdatedBy = OutboundValidationDetailInfo.CreatedBy;
                OutboundValidationDetailInfo.CreatedDate = DateTime.Now;
                OutboundValidationDetailInfo.UpdatedDate = DateTime.Now;
                if (OutboundValidationDetailDA.InsertOutboundValidationDetail(OutboundValidationDetailInfo, this.dataBaseID[0]) > 0)
                {
                    lineID = OutboundValidationDetailInfo.SeqNo.ToString();
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return lineID;
        }

        public string UpdateDetail(object obj)
        {
            string LineID = string.Empty;
            try
            {
                OutboundValidationDetailInfo OutboundValidationDetailInfo = obj as OutboundValidationDetailInfo;
                OutboundValidationDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                OutboundValidationDetailInfo.UpdatedDate = DateTime.Now;
                if (OutboundValidationDetailDA.UpdateOutboundValidationDetail(OutboundValidationDetailInfo, this.dataBaseID[0]) > 0)
                {
                    LineID = OutboundValidationDetailInfo.ValidationID + " , " + OutboundValidationDetailInfo.SeqNo;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

